﻿#include "blua.h"

static int test_func(lua_State* p_state )
{
	pblua_t p_lua = 0;

	lua_getallocf( p_state, (void**)&p_lua );

	printf( "hello world\n" );
	return 0;
}

static int test_func2( lua_State* p_state )
{
	pblua_t p_lua = 0;
	lua_getallocf( p_state, (void**)&p_lua );
	
	bl_pushint( p_lua, 250 );
	return 1;
}

static int test_bl_func( pblua_t p_lua )
{
	printf( "hello bl func\n" );
	return 0;
}

static int test_bl_func1( pblua_t p_lua )
{
	printf( "hello bl func1\n" );
	return 0;
}

static int onenum( pblua_t p_lua )
{
	int b_ret = 0;
	char sz_name[128];
	size_t un_len = 128;

	bb_error( bl_tostr( p_lua, -2, sz_name, &un_len ) );
	printf( "%s\n", sz_name );

	b_ret = 1;
	bb_exit
	return b_ret;
}

int main( int argn, char** argv )
{
	int b_ret = 1;
	pblua_t p_lua = NULL;
	int n_testvalue = 0;
	blua_number d_testvalue = 0.0f;
	char ca_testbuffer[256];
	size_t un_len = 256;

#if 0
	luareg_t testlib[] = 
	{
		{ "test1", test_func },
		{ "test2", test_func2 },
		{ NULL, NULL },
	};
	bluareg_t testblib[] = 
	{
		{ "btest1", test_bl_func },
		{ "btest2", test_bl_func1 },
		{ "btest3", test_bl_func1 },
		{ NULL, NULL },
	};
#endif

	bb_error( p_lua = bl_allocstate( NULL ) );

#if 0 && test_enum_table
	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_enumtable( p_lua, LUA_GLOBALSINDEX, onenum ) );
#endif

#if 0 && test_call
	bb_error( bl_pushfunction( p_lua, test_func2 ) );
	bb_error( bl_pushint( p_lua, 10 ) );
	bb_error( bl_pushstring( p_lua, "hell" ) );
	bb_error( bl_call( p_lua, 2, 8000 ) );
	bb_error( bl_dumpstack( stdout, p_lua ) );
#endif

#if 0 && test_is_and_to
	bb_error( bl_pushnil( p_lua ) );
	bb_error( bl_pushint( p_lua, 100 ) );
	bb_error( bl_pushstring( p_lua, "bey0ndc00de" ) );
	bb_error( bl_pushtable( p_lua ) );
	bb_error( bl_pushint( p_lua, 200 ) );
	bb_error( bl_pushstring( p_lua, "key" ) );
	bb_error( bl_dumpstack( stdout, p_lua ) );

	printf( "%d\n", bl_isstring( p_lua, -1 ) );
	printf( "%d\n", bl_isstring( p_lua, -3 ) );
	printf( "%d\n", bl_isstring( p_lua, 0 ) );
	printf( "%d\n", bl_isstring( p_lua, -1000 ) );

	printf( "%d\n", bl_isnumber( p_lua, -1000 ) );
	printf( "%d\n", bl_isstring( p_lua, -3000 ) );
	printf( "%d\n", bl_istable( p_lua, -40000 ) );
#endif

#if 0 && test_is_and_to_function
	bb_error( bl_exportblib( p_lua, "KGamebase.libtestb", testblib, 0 ) );
	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_dumpstack( stdout, p_lua ) );
#endif

#if 0 && test_export_lib
	bb_error( bl_exportlib( p_lua, "KGamebase.libtest", testlib, 0 ));
	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_setfuncbyname( p_lua, "KGamebase.libtest.test2", &test_func ) );
	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_dumpstack( stdout, p_lua ) );
#endif

#if 0 && test_export_function
	bb_error( bl_setfuncbyname( p_lua, "KGamebase.test_func", test_func ) );
	bb_error( bl_setfuncbyname( p_lua, "KGamebase.test_func1", test_func ) );
	bb_error( bl_setfuncbyname( p_lua, "KGamebase.test_func2", test_func ) );
	bb_error( bl_setfuncbyname( p_lua, "KGamebase.test_func3", test_func2 ) );
	bb_error( bl_setbfuncbyname( p_lua, "KGamebase.test_bl_func", test_bl_func ) );
	bb_error( bl_setbcclosurebyname( p_lua, "KGamebase.test_bl_func1", test_bl_func1, 5 ) );

	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
#endif
	
#if 0 && test_setintbyname_getintbyname
	bb_error( bl_loadfile( p_lua, "beyondlua.lua" ) );

	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_pushint( p_lua, 1 ) );
	bb_error( bl_pushint( p_lua, 2 ) );
	bb_error ( bl_pushstring( p_lua, "bey0ndc00d" ) );

	bb_error( bl_getintbyname( p_lua, "KGamebase.nVersion", &n_testvalue ) );
	bb_error( bl_setintbyname( p_lua, "KGamebase.nVersion", 10000 ) );
	bb_error( bl_getintbyname( p_lua, "KGamebase.nVersion", &n_testvalue ) );

	bb_error( bl_pushstring( p_lua, "KGamebase" ) );
	bb_error( bl_gettable( p_lua, LUA_GLOBALSINDEX ) );
	bb_error( bl_pushstring( p_lua, "nVersion" ) );
	bb_error( bl_gettable( p_lua, -2 ) );
	bl_dumpstack( stdout, p_lua );
#endif

#if 0 && test_luasettable_and_gettable
	bb_error( bl_pushtable( p_lua ) );
	bb_error( bl_pushstring( p_lua, "index" ) );
	bb_error( bl_pushint( p_lua, 100 ) );
	bb_error( bl_settable( p_lua, 1) );

	bb_error( bl_pushstring( p_lua, "index" ) );
	bb_error( bl_gettable( p_lua, 1 ) );

	bb_error( bl_toint( p_lua, -1, &n_testvalue ) );
#endif

#if 0
	bb_error( bl_dump( stdout, p_lua ) );
	bb_error( bl_loadfile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_dump( stdout, p_lua ) );
	bb_error( bl_dofile( p_lua, "beyondlua.lua" ) );
	bb_error( bl_dump( stdout, p_lua ) );

	bb_error( bl_getstringbyname( p_lua, "sz_test1", ca_testbuffer, &un_len ) );
	printf( "sz_test1:%s\n", ca_testbuffer );
	
	bb_error( bl_getintbyname( p_lua, "tb_test1.tb_3.n_2", &n_testvalue ) );
	printf( "tb_test1.n_1:%d\n", n_testvalue );

	bb_error( bl_getintbyname( p_lua, "tb_test1.n_1", &n_testvalue ) );
	printf( "tb_test1.n_1:%d\n", n_testvalue );
#endif

	bb_release( p_lua, bl_freestate );

	b_ret = 0;
	bb_exit
	return b_ret;
}
/***********************************

2010/11/23-xx:xx @ corp.king
bl_main.c
bey0ndc00de
utf-8 --with-sig

************************************/
/*last for linux*/
